#include "tcp.h"
#include "ui_tcp.h"

TCP::TCP(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::TCP)
{
	ui->setupUi(this);
	this->setWindowIcon(QIcon(":/network.png"));
	this->setWindowTitle("本地网络调试");
	resize(1200,600);

	// 设置client文字提示
	ui->textEdit_client_read->setPlaceholderText("收到的消息:");
	ui->textEdit_client_write->setPlaceholderText("填入需要发送的消息");
	//设置server文字提示
	ui->textEdit_server_read->setPlaceholderText("收到的消息:");
	ui->textEdit_server_write->setPlaceholderText("填入需要发送的消息");

	//建立监听套接字
	tcp_server = new QTcpServer(this);
	tcp_client_socket = new QTcpSocket(this);
	//设置服务端默认绑定端口
	ui->lineEdit_server_ip_port->setText("2234");
	//设置客户端默认连接目的主机的IP和端口
	ui->lineEdit_client_ip_port->setText("127.0.0.1:2234");
	//服务端点击绑定按钮，开始绑定地址和ip
	connect(ui->pushButton_server_bind,&QPushButton::clicked,[=](){
		if(this->bind_button)
		{
			ui->textEdit_server_read->append("系统：取消绑定成功");
			tcp_server->close();
			ui->pushButton_server_bind->setText("绑定端口");
			if(tcp_server_socket != nullptr)
			{
				//服务端套接字置空
				tcp_server_socket->disconnect();
				tcp_server_socket->close();
				tcp_server_socket = nullptr;
				//改变按钮状态
				ui->pushButton_client_connect->setEnabled(true);
				ui->textEdit_client_read->append("系统：服务端关闭端口，连接已断开");
				ui->textEdit_server_read->append("系统：服务端关闭端口，连接已断开");
			}
			//改变按钮状态和客户端连接状态
			this->bind_button = false;
			this->tcp_client = false;
		}
		else
		{
			ui->textEdit_server_read->append("系统：绑定成功");
			tcp_server->listen(QHostAddress::Any, ui->lineEdit_server_ip_port->text().toUShort());
			ui->pushButton_server_bind->setText("取消绑定");
			this->bind_button = true;
		}
	});

	//监听套接字，服务端开始监听来自外部的连接
	connect(tcp_server,&QTcpServer::newConnection,[=](){
		// 取出建立好的套接字
		tcp_server_socket = tcp_server->nextPendingConnection();
		QString ip = tcp_server_socket->peerAddress().toString();
		quint16 port = tcp_server_socket->peerPort();
		QString ip_port = QString("系统：与客户端 %1 : %2 建立连接成功").arg(ip).arg(port);
		ui->textEdit_server_read->append(ip_port);

		//通信套接字
		connect(tcp_server_socket,&QTcpSocket::readyRead,[=](){
			//读出套接字内容
			QByteArray content = tcp_server_socket->readAll();
			ui->textEdit_server_read->append(content);
		});

		//当服务端断开连接
		connect(tcp_server_socket,&QTcpSocket::disconnected,[=](){
			//服务端套接字置空
			tcp_server_socket = nullptr;
			//改变客户端连接状态
			this->tcp_client = false;
			//改变按钮状态
			ui->pushButton_client_connect->setEnabled(true);
		});
	});

	//当客户端点击发起连接时
	connect(tcp_client_socket,&QTcpSocket::connected,[=](){
		//建立连接成功
		ui->textEdit_client_read->append("系统：与服务端建立连接成功");
		//改变连接状态
		this->tcp_client = true;
		//改变按钮状态
		ui->pushButton_client_connect->setEnabled(false);

		//客户端通信套接字
		connect(tcp_client_socket,&QTcpSocket::readyRead,[=](){
			//读出套接字内容
			QByteArray content = tcp_client_socket->readAll();
			ui->textEdit_client_read->append(content);
		});

		//当客户端断开连接时
		connect(tcp_client_socket,&QTcpSocket::disconnected,[=](){
			//服务端套接字置空
			tcp_server_socket = nullptr;
			//改变客户端连接状态
			this->tcp_client = false;
			//改变按钮状态
			ui->pushButton_client_connect->setEnabled(true);
		});
	});
}

TCP::~TCP()
{
	delete ui;
}

void TCP::on_pushButton_server_send_clicked()
{
	if(tcp_server_socket == nullptr)
	{
		ui->textEdit_server_read->append("系统：没有与客户端建立连接");
		return;
	}
	//获取编辑区内容
	QString str = ui->textEdit_server_write->toPlainText();
	//给对方发送数据
	tcp_server_socket->write(str.toUtf8().data());
	//清空文本框
	ui->textEdit_server_write->clear();
}

void TCP::on_pushButton_server_close_clicked()
{
	if(tcp_server_socket == nullptr)
	{
		ui->textEdit_server_read->append("系统：没有与客户端建立连接");
		return;
	}
	//主动和客户端断开连接
	tcp_server_socket->disconnect();
	tcp_server_socket->close();
	//显示断开连接提示
	ui->textEdit_client_read->append("系统：服务端断开连接");
	ui->textEdit_server_read->append("系统：服务端断开连接");
}

void TCP::on_pushButton_client_connect_clicked()
{
	//主动和服务端建立连接
	QString ip = ui->lineEdit_client_ip_port->text().split(":")[0];
	quint16 port = ui->lineEdit_client_ip_port->text().split(":")[1].toUShort();
	tcp_client_socket->connectToHost(QHostAddress(ip),port);
	qDebug()<<"连接远程主机";
}

void TCP::on_pushButton_client_send_clicked()
{
	if(!this->tcp_client)
	{
		ui->textEdit_client_read->append("系统：没有与服务端建立连接");
		return;
	}
	//获取编辑框内容
	QString str = ui->textEdit_client_write->toPlainText();
	tcp_client_socket->write(str.toUtf8().data());
	//清空文本框
	ui->textEdit_client_write->clear();
}

void TCP::on_pushButton_client_close_clicked()
{
	if(!this->tcp_client)
	{
		ui->textEdit_client_read->append("系统：没有与服务端建立连接");
		return;
	}
	//断开客户端连接
	tcp_client_socket->disconnectFromHost();
	tcp_client_socket->close();
	//显示断开连接提示
	ui->textEdit_client_read->append("系统：客户端断开连接");
	ui->textEdit_server_read->append("系统：客户端断开连接");
}
